package com.xiaoyu.lanling.feature.voicecall.listener

import `in`.srain.cube.app.lifecycle.AppActivityLifecycleCallbacks
import android.content.Context
import android.os.PowerManager
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.lanling.event.videomatch.VideoMatchReceiveEvent
import com.xiaoyu.lanling.event.voicematch.VoiceMatchReceiveEvent
import com.xiaoyu.lanling.feature.videocall.util.VideoCallNotificationUtils
import com.xiaoyu.lanling.feature.videomatch.util.VideoMatchNotificationUtils
import com.xiaoyu.lanling.feature.voicecall.data.CallData
import com.xiaoyu.lanling.feature.voicecall.util.VoiceCallNotificationUtils
import com.xiaoyu.lanling.feature.voicematch.util.VoiceMatchNotificationUtils
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lib_av.datamodel.CallParams
import com.xiaoyu.lib_av.listener.AbstractVoiceCallLifecycleListener

class CallLifecycleListener private constructor() : AbstractVoiceCallLifecycleListener(), AppActivityLifecycleCallbacks.OnLifecycleListener {

    private var mTempReceiveCallParams: CallParams? = null

    override fun onCallCanceled(callParams: CallParams) {
        when (callParams.type) {
            CallParams.VOICE_CALL -> VoiceCallNotificationUtils.instance.dismissCallNotification()
            CallParams.VOICE_MATCH -> VoiceMatchNotificationUtils.instance.dismissCallNotification()
            CallParams.VIDEO_CALL -> VideoCallNotificationUtils.instance.dismissNotification()
            CallParams.VIDEO_MATCH -> VideoMatchNotificationUtils.instance.dismissCallNotification()
        }

        AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
        mTempReceiveCallParams = null
    }

    override fun onReceiveCall(fromUserId: String, params: CallParams, runnable: Runnable?) {
        processReceiveCallParams(params)
    }

    private fun processReceiveCallParams(params: CallParams) {
        when (params.type) {
            CallParams.VOICE_CALL -> processReceiveVoiceCall(params)
            CallParams.VOICE_MATCH -> processReceiveVoiceMatch(params)
            CallParams.VIDEO_CALL -> processReceiveVideoCall(params)
            CallParams.VIDEO_MATCH -> processReceiveVideoMatch(params)
        }
    }

    private fun processReceiveVoiceMatch(params: CallParams) {
        VoiceMatchReceiveEvent(params).post()
        val powerManager = AppContext.getContext().getSystemService(Context.POWER_SERVICE) as PowerManager
        if (!powerManager.isInteractive || !AppActivityLifecycleCallbacks.getInstance().isAppInForeground) {
            mTempReceiveCallParams = params
            AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
            AppActivityLifecycleCallbacks.getInstance().addLifecycleListener(this)
        }
    }

    private fun processReceiveVideoMatch(params: CallParams) {
        VideoMatchReceiveEvent(params).post()
        val powerManager = AppContext.getContext().getSystemService(Context.POWER_SERVICE) as PowerManager
        if (!powerManager.isInteractive || !AppActivityLifecycleCallbacks.getInstance().isAppInForeground) {
            mTempReceiveCallParams = params
            AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
            AppActivityLifecycleCallbacks.getInstance().addLifecycleListener(this)
        }
    }

    private fun processReceiveVoiceCall(params: CallParams) {
        val powerManager = AppContext.getContext().getSystemService(Context.POWER_SERVICE) as PowerManager
        val isFromSelf = UserData.getInstance().isSelf(params.fromUser.uid)

        if (powerManager.isInteractive && AppActivityLifecycleCallbacks.getInstance().isAppInForeground) {
            Router.instance.gotoChatCallActivity(AppContext.getContext(), params.getFuid(isFromSelf), "", false)
        } else {
            mTempReceiveCallParams = params
            AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
            AppActivityLifecycleCallbacks.getInstance().addLifecycleListener(this)
            VoiceCallNotificationUtils.instance.showCallNotification(params.getFuid(isFromSelf), isCaller = false, isReceive = true)
        }
    }

    private fun processReceiveVideoCall(params: CallParams) {
        val powerManager = AppContext.getContext().getSystemService(Context.POWER_SERVICE) as PowerManager
        val isFromSelf = UserData.getInstance().isSelf(params.fromUser.uid)

        if (powerManager.isInteractive && AppActivityLifecycleCallbacks.getInstance().isAppInForeground) {
            mTempReceiveCallParams = null
            Router.instance.gotoVideoCallActivity(AppContext.getContext(), params, "", false)
        } else {
            mTempReceiveCallParams = params
            AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
            AppActivityLifecycleCallbacks.getInstance().addLifecycleListener(this)
            VideoCallNotificationUtils.instance.showNotification(params, isCaller = false, isReceive = true)
        }
    }

    override fun onAppToForeground() {
        val params = mTempReceiveCallParams ?: return
        AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
        processReceiveCallParams(params)
    }

    override fun onAppToBackground() {
        AppActivityLifecycleCallbacks.getInstance().removeLifecycleListener(this)
        mTempReceiveCallParams = null
    }

    override fun onTokenExpired() {
        CallData.sLoginRunnable.run()
    }

    companion object {
        @JvmStatic
        val instance by lazy { CallLifecycleListener() }
    }
}